package springboot.study.project.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springboot.study.project.common.Result;
import springboot.study.project.dto.StudentDto;
import springboot.study.project.model.Student;
import springboot.study.project.service.StudentService;
import springboot.study.project.util.common.ResultUtil;

import java.util.Map;

/**
 * @ClassName: StudentController
 * @Description: 学生相关控制器
 * @Author: liu-hao
 * @Date: 2021-03-28 23:58
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    @RequestMapping("/getStudentInfo")
    public String getStudentInfo(@RequestParam(name = "stuId") String stuId, // 学号
                                 @RequestParam(name = "stuName") String stuName, // 姓名
                                 @RequestParam(name = "clazzId") String clazzId, // 班级
                                 @RequestParam(name = "page", defaultValue = "1") int pageNum,
                                 @RequestParam(name = "limit", defaultValue = "10") int pageSize) {

        int offset = (pageNum - 1) * pageSize;
        Map<String, Object> stuInfo = studentService.getStuInfo(stuId, stuName, clazzId, offset, pageSize);

        return JSON.toJSONString(stuInfo);
    }

    @RequestMapping("/getStuInfoById")
    public Result getStuInfoById(@RequestParam(name = "stuId") String stuId) {
        StudentDto studentDTO = studentService.getStuInfoById(stuId);
        if (studentDTO == null) {
            return ResultUtil.failure("该学生不存在！");
        }
        return ResultUtil.success(studentDTO);
    }

    @RequestMapping("/removeStuByStuId")
    public Result removeStuByStuId(@RequestParam(name = "stuId") String stuId) {
        boolean result = studentService.removeStudentInfoById(stuId);
        if (!result) {
            return ResultUtil.failure("该学生不存在！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/addStudentInfo")
    public Result addStudentInfo(@RequestBody Student student) {
        if (student == null) {
            return ResultUtil.failure("请填写学生完整信息！");
        }
        boolean result = studentService.addStudentInfo(student);
        if (!result) {
            return ResultUtil.failure("添加学生失败！");
        }
        return ResultUtil.success();
    }
    @RequestMapping("/modifyStuInfo")
    public Result modifyStuInfo(@RequestBody Student student) {
        if (student == null) {
            return ResultUtil.failure("请填写学生完整信息！");
        }
        boolean result = studentService.modifyStuInfo(student);
        if (!result) {
            return ResultUtil.failure("修改学生信息失败！");
        }
        return ResultUtil.success();
    }

    @RequestMapping("/checkStuIsExisted")
    public Result checkStuIsExisted(String stuId) {
        boolean result = studentService.checkStuIsExisted(stuId);
        return ResultUtil.success(result);
    }
}
